//
//  TXP_MainViewController.m
//  StayAwhile
//
//  Created by dllo on 16/1/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TXP_MainViewController.h"
#import "TXP_LeftViewController.h"
#import "TXPHomePageViewController.h"
#import "TXP_NavigationController.h"
#import "TXPCenterView.h"
@interface TXP_MainViewController ()<UIGestureRecognizerDelegate,TXPHomePageViewControllerDelegate>

@property (nonatomic, strong) TXPHomePageViewController *centerViewController;  //内容
@property (nonatomic, strong) TXP_LeftViewController *leftViewController;      //菜单

@property (nonatomic, strong) UIView *centerView; //内容
@property (nonatomic, strong) UIView *leftView;   //菜单

@property (nonatomic, assign) CGPoint panGestureStartLocation;     //手势坐标(点)
@property (nonatomic, strong) UIPanGestureRecognizer *panGesture;  //手势

@property (nonatomic, strong) TXPHomePageViewController *oldCenterViewController;
@property (nonatomic, strong) UIButton *tapCloseButton;
@end

@implementation TXP_MainViewController
- (instancetype)initWithCenterViewController:(TXPHomePageViewController *)centerCtl leftViewController:(TXP_LeftViewController *)leftCtl{
    self = [super init];
    if (self) {
        TXP_NavigationController *navCtl = [[TXP_NavigationController alloc] initWithRootViewController:centerCtl];
        //给内容视图赋值
        self.centerViewController = (id)navCtl;
        //给菜单区赋值
        self.leftViewController = leftCtl;
        self.oldCenterViewController = centerCtl;
        centerCtl.delegate = self;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.hidden=YES;
    [self setUpCenterView];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
    {
        UIPanGestureRecognizer *pan = (UIPanGestureRecognizer*)gestureRecognizer;
        CGFloat x = self.oldCenterViewController.contentScrollView.contentOffset.x;
        if ([pan velocityInView:self.view].x > 0.0f && x == 0.0f) {
            [self setUpCenterView];//如果没有他 刚开始那就没有手势
        }
    }
    return YES;
}

#pragma mark -- 设置中心的view
- (void)setUpCenterView{
    //初始化centerView
    self.centerView = [[UIView alloc]initWithFrame:self.view.bounds];
    self.centerViewController.view.bounds = self.view.bounds;
    
    //视图添加
    [self.centerView addSubview:self.centerViewController.view];
    [self addChildViewController:self.centerViewController];
    //当从一个视图控制容器中添加或者移除viewController后，该方法被调用。
    [self.centerViewController didMoveToParentViewController:self];
    
    //将centerView添加到自己的View上
    [self.view addSubview:self.centerView];
    
    //初始化手势
    self.panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureEvent:)];
    self.panGesture.delegate = self;
    
    //给centerView添加手势
    [self.centerView addGestureRecognizer:self.panGesture];
    
}


#pragma mark -- 会捕捉到边缘手势 滑动就会一直执行
- (void)panGestureEvent:(UIPanGestureRecognizer*)panGesture {
    //获取手势的类别
    UIGestureRecognizerState state = panGesture.state;
    //该方法返回在横坐标上、纵坐标上拖动了多少
    CGPoint translation = [panGesture translationInView:self.view];
    //检测移动速度
    CGPoint velocity = [panGesture velocityInView:self.view];
    
    switch (state) {
            
            //识别器接收到触摸并识别出是它的手势时处于这个状态。响应方法将在下个循环步骤中被调用
        case UIGestureRecognizerStateBegan:
            [self prepareingLeftView];//调出菜单视图
            [self showShadow:YES];
            break;
            
            //识别器识别出手势之后开始变化执行
        case UIGestureRecognizerStateChanged:{
            static CGRect centerFrame;
            centerFrame = self.centerView.frame;
            centerFrame.origin.x += translation.x;
            if (centerFrame.origin.x < 0) {
                centerFrame.origin.x = 0;
            }
            self.centerView.frame = centerFrame;
        }
            break;
            
            //识别器在识别到作为当前手势结束信号的触摸时处于这个状态。响应方法将在下个循环步骤中被调用
            //并且 识别器将重置为possible状态
        case UIGestureRecognizerStateEnded:
        {
            //滑动结果(划少了 划多了) 动画效果处理
            if (self.centerView.frame.origin.x > 10 && velocity.x>0) {
                [self openingLeftController];
            } else {
                [self closeingLeftController];
            }
        }
            break;
            
        default:
            break;
    }
    [panGesture setTranslation:CGPointZero inView:self.view];
}

#pragma mark -- 添加菜单视图
- (void)prepareingLeftView{
    //初始化视图
    self.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    self.leftViewController.view.frame = self.view.bounds;
    //添加视图
    [self.leftView addSubview:self.leftViewController.view];
    [self addChildViewController:self.leftViewController];
    
    //当从一个视图控制容器中添加或者移除viewController后，该方法被调用。
    [self.leftViewController willMoveToParentViewController:self];
    //将菜单视图插入到内容视图的下面
    [self.view insertSubview:self.leftView belowSubview:self.centerView];
    
}

#pragma mark -- 打开菜单视图
- (void)openingLeftController {
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:10 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        self.centerView.frame = CGRectMake(270, 0, self.view.bounds.size.width, self.view.bounds.size.height);
        
    } completion:^(BOOL finished) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeingLeftController)];
        [self.centerView addGestureRecognizer:tap];
        self.oldCenterViewController.contentScrollView.userInteractionEnabled = NO;
        [self showShadow:YES];
    }];
}

#pragma mark -- 关闭菜单视图
- (void)closeingLeftController {
    
    [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:10 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        self.centerView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
    } completion:^(BOOL finished) {
        [self.leftViewController willMoveToParentViewController:nil];
        [self.leftViewController.view removeFromSuperview];
        [self.leftViewController removeFromParentViewController];
        self.oldCenterViewController.contentScrollView.userInteractionEnabled = YES;
        [self.leftView removeFromSuperview];
        
    }];
}

#pragma mark -- 阴影效果
- (void)showShadow:(BOOL)val {
    if (!self.centerViewController) return;
    
    self.centerViewController.view.layer.shadowOpacity = val ? 0.8f : 0.0f;
    if (val) {
        self.centerViewController.view.layer.cornerRadius = 4.0f;
        self.centerViewController.view.layer.shadowOffset = CGSizeZero;
        self.centerViewController.view.layer.shadowRadius = 4.0f;
        self.centerViewController.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
    }
    
}

#pragma mark -- 代理
#pragma mark - RootViewControllerCheckleftBarButtonItemDelegate
- (void)checkleftBarButtonItemRootViewController:(UIViewController *)viewController
{
    [self prepareingLeftView];
    self.centerView.frame.origin.x > 0 ? [self closeingLeftController]:[self openingLeftController];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
